//
//  YQPPTransformButton.m
//  项目基础框架
//
//  Created by yangqian on 16/3/10.
//  Copyright © 2016年 NetEase-yangqian. All rights reserved.
//

#import "YQPPTransformButton.h"

static CGFloat const kTransformRotateRate = 0.05 * M_PI;

@interface YQPPTransformButton ()

@property (strong,   nonatomic) UIImageView *transformImage;
@property (copy,   nonatomic) NSString    *imageName;
@property (copy,   nonatomic) NSString    *trasImageName;
@property (assign, nonatomic) CGFloat currentOffset;
@property (assign, nonatomic) CGFloat beginOffset;
@property (assign, nonatomic) BOOL touchEnd;

@end

@implementation YQPPTransformButton


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

+ (instancetype)transformButtonWithFrame:(CGRect)frame imageName:(NSString *)imageName transformImage:(NSString *)traImage {
    YQPPTransformButton *transform = [[self alloc]initWithFrame:frame];
    [transform setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    transform.transformImage.image = [UIImage imageNamed:traImage];
    transform.trasImageName = traImage;
    transform.imageName = imageName;
    return transform;
}

- (void)startTransform {
    _touchEnd = NO;
    if (self.transformImage.image != nil) {
        [_transformImage.layer removeAllAnimations];
        [self setImage:[[UIImage alloc]init] forState:UIControlStateNormal];
        [self addSubview:self.transformImage];
    }
}

- (void)endTransform {
    if (_touchEnd) {
        [_transformImage.layer removeAllAnimations];
        [_transformImage removeFromSuperview];
        [self setImage:[UIImage imageNamed:_imageName] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.35 animations:^{
            CGRect frame = self.frame;
            frame.origin.y = -30;
            self.frame = frame;
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (void)continueTransform {
    if (_trasImageName != nil && _touchEnd) {
        [_transformImage.layer removeAllAnimations];
        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
        rotationAnimation.duration = 0.8;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = CGFLOAT_MAX;
        [_transformImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
}

- (void)transformWithClockwise:(BOOL)clockwise {
    if (_trasImageName != nil) {
        _transformImage.transform = CGAffineTransformRotate(_transformImage.transform,clockwise ? kTransformRotateRate : -kTransformRotateRate);
    }
}

// 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _beginOffset = scrollView.contentOffset.y;
    [self startTransform];
}

// scrollview 停止滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if(decelerate) _touchEnd = YES;
    if (scrollView.contentOffset.y - _beginOffset < -120) {
        [self continueTransform];
        if ([self.deleagte respondsToSelector: @selector(endDrag)]) {
            [self.deleagte endDrag];
        }
    }
}

// 判断滚动方向
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > 0) return;
    if (_currentOffset < scrollView.contentOffset.y) { // 向上滚动
        if(![_transformImage.layer animationForKey:@"rotationAnimation"]) [self transformWithClockwise:YES];
    } else {
        if(![_transformImage.layer animationForKey:@"rotationAnimation"]) [self transformWithClockwise:NO];
    }
    _currentOffset = scrollView.contentOffset.y;
    if (scrollView.contentOffset.y < -120) {
        CGRect frame = self.frame;
        frame.origin.y = scrollView.contentOffset.y + 90;
        self.frame = frame;
    }else if([_transformImage.layer animationForKey:@"rotationAnimation"]) {
        CGRect frame = self.frame;
        frame.origin.y = scrollView.contentOffset.y + 90;
        self.frame = frame;
    }
}

- (UIImageView *)transformImage {
    if (_transformImage == nil) {
        _transformImage = [[UIImageView alloc]initWithFrame:self.bounds];
    }
    return _transformImage;
}
@end
